package com.hnl.action.talent;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.EducationDAO;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.to.Education;
import com.hnl.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class DeleteEducation extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory
			.getLogger(DeleteEducation.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {	
			// delete
			Integer talentID=	Integer.parseInt( req.getParameter(ATTRIBUTE_TALENT_ID));	
			String delString = req.getParameter(ATTRIBUTE_DELETE_LIST);
			EducationDAO educationDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getEducationDAO();
			String[] delLists = delString.split(",");
			for(String eduId:delLists){				
				Integer eID=Integer.parseInt(eduId);
				educationDAO.deleteEducation(eID);
			}
			
			Collection<Education> eduCollection = new Vector<Education>();
			if(talentID!=null&&talentID!=0){
				EducationDAO eduDAO = HNLDAOFactory.getDAOFactory(
						HNLDAOFactory.HNLDAOFACTORY).getEducationDAO();	
				eduCollection=eduDAO.loadEducation(Integer.valueOf(talentID));		
			}
			
			writer.println(JSONResponseUtil.getJSONObjectString(eduCollection));			
			
		} catch (DAOException e) {
			LOG.error("Error to delete Talent", e);
		} finally {
			writer.close();
		}
	}

}
